Ext.define("Rms.history.History", {
	singleton : true,
	init : function() {
		Ext.util.History.init(function() {this.initialNavigate()}, this);
		Ext.util.History.on("change", function(token) {this.navigate(token)}, this)
	},
	
	initialNavigate : function() {
	  this.navigate(Ext.util.History.getToken())
	},
	
	navigate : function(token) {
		var user = Ext.state.Manager.get('user');
		var url = this.parseToken(token);
		if (url.type == 'login' || !token || !user) {
			Rms.App.getController('Login').loadLogin(url.url);
		} else {
			if (url.type == 'main') {
				Rms.App.getController('Main').loadMain(url.url);
			}
		}
	},
	
	parseToken : function(token) {
		var url = token ? token.match(/((login|main)(\/(.*))?\??(.*)?)/) : null;
		return url ? { type : url[2], url : "#" + url[1]} : {}
	}
});